package gruppe7.data;

import java.util.HashMap;
import java.util.Iterator;

public class HashMapUserProvider implements UserProvider {

	private HashMap<User, PasswordHash> userMap;
	
	public HashMapUserProvider() {
		userMap = new HashMap<User, PasswordHash>();
		
		String[][] testUsers = {
				
				{"Peter", "Pan"},
				{"Hank", "Williams"},
				{"Betty", "b4711"},
				{"Lotte", "Lenya"},
				{"Lilly", "hurra"}
		};
		
		for (int i = 0; i < testUsers.length; i ++) {
			String username = testUsers[i][0];
			String passwordClear = testUsers[i][1];
			
			userMap.put(new User(username, User.Type.TYPE_REGULAR), new PasswordHash(passwordClear));
		}
		
		// og en admin in the mix
		userMap.put(new User("admin", User.Type.TYPE_ADMIN), new PasswordHash("password"));
	}
	
	public void addUser(User newUser, PasswordHash hash) throws UserException {
		
	}

	public User authenticateUser(String username, PasswordHash hash) throws UserException {

		Iterator<User> it = userMap.keySet().iterator();
		
		while (it.hasNext()) {
			User existingUser = it.next();
			PasswordHash existingUserHash = userMap.get(existingUser);
			
			if (existingUser.getUsername().equals(username) && existingUserHash.getHash().equals(hash.getHash())) return existingUser;
		}
		
		throw new UserException("User " + username + " not found");
	}

	
}
